<?php
/**
 * 
 */
namespace app\common\query;

use app\common\model\AgentTypeOffset as AgentTypeOffsetModel;

use app\common\cache\CacheData;

class AgentTypeOffset
{
    
    public function cacheData(){
        $list = $this->all();
        return collection($list)->toArray();
    }

    /**
     * @return array
     */
	public static function agentTypeOffset(){
        $CACHE = CacheData::getInstance('AgentTypeOffset');
        $cacheData = $CACHE -> readCache();
        $reset = [];
        $level = Level::levelData();
        $type = AgentType::agentTypeData();
        $coin = Coin::coinData();
        foreach ($cacheData as $v) {
            $v['level_name'] = $level[$v['level_id']]['level_name'];
            $v['type_name'] = $type[$v['type_id']]['type_name'];
            $v['origin_name'] = $coin[$v['origin']]['coin_name'];
            $v['target_name'] = $coin[$v['target']]['coin_name'];
            $reset[$v['type_id']][] = $v;
        }
        foreach ($reset as $k => $v) {
            $reset[$k] = array_combine(array_column($v, 'level_id'), $v);
        }
        return $reset;
    }

    /**
     * @param $type_id
     * @param $level_id
     * @return mixed|null
     */
    public static function offsetConf($type_id, $level_id)
    {
        if (false == $type_id) {
           return null;
        }
        $data =  self::agentTypeOffset();
        if (! array_key_exists($type_id, $data)) {
            return null;
        }
        $offset = $data[$type_id];
        if (false == $level_id) {
            return $offset;
        }
        if (! array_key_exists($level_id, $offset)) {
            return null;
        }
        return $offset[$level_id];
    }

}